<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 任务总执行时长（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>任务编排服务负责对任务进行组合调度。</p> 
<p>参与编排的任务有两种类型&#xff0c;其中一种执行时长为taskA&#xff0c;另一种执行时长为taskB。</p> 
<p>任务一旦开始执行不能被打断&#xff0c;且任务可连续执行。</p> 
<p>服务每次可以编排num个任务。</p> 
<p>请编写一个方法&#xff0c;生成每次编排后的任务所有可能的总执行时长。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第1行输入分别为第1种任务执行时长taskA&#xff0c;</p> 
<p>第2种任务执行时长taskB&#xff0c;</p> 
<p>这次要编排的任务个数num&#xff0c;以逗号分隔。</p> 
<p></p> 
<p>注&#xff1a;每种任务的数量都大于本次可以编排的任务数量</p> 
<ul><li>0 &lt; taskA</li><li>0 &lt; taskB</li><li>0 &lt;&#61; num &lt;&#61; 100000</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>数组形式返回所有总执行时时长&#xff0c;需要按从小到大排列。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1,2,3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">[3, 4, 5, 6]</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题看上去是求解全排列&#xff0c;但是实际上无论如何排列&#xff0c;其实就只是两种类型任务的排列&#xff0c;而且本题要求任务编排的执行总时长&#xff0c;这其实就是就每种排列的和&#xff0c;因此其实我们不需要求解排列&#xff0c;只需要求解该排列对应的组合即可</p> 
<p>比如用例中&#xff0c;有三个任务&#xff0c;那么有如下组合&#xff1a;</p> 
<ul><li>三个1</li><li>两个1&#xff0c;一个2</li><li>一个1&#xff0c;两个2</li><li>三个2</li></ul> 
<p>无论每个组合&#xff0c;能编排成几个排列&#xff0c;其实执行总时长&#xff0c;即排列的和都是一样的</p> 
<ul><li>三个1&#xff1a;<span style="color:#fe2c24;">和为3</span></li><li>两个1&#xff0c;一个2&#xff1a;<span style="color:#fe2c24;">和为4</span></li><li>一个1&#xff0c;两个2&#xff1a;<span style="color:#fe2c24;">和为5</span></li><li>三个2&#xff1a;<span style="color:#fe2c24;">和为6</span></li></ul> 
<p>因此&#xff0c;本题其实就是求解两种类型任务各自可能的数量。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const [taskA, taskB, num] &#61; line.split(&#34;,&#34;).map(Number);
  console.log(getResult(taskA, taskB, num));
});

function getResult(taskA, taskB, num) {
  if (num &#61;&#61; 0) return [];

  if (taskA &#61;&#61;&#61; taskB) {
    return &#96;[${taskA * num}]&#96;;
  }

  const ans &#61; new Set();
  for (let i &#61; 0; i &lt;&#61; num; i&#43;&#43;) {
    ans.add(taskA * i &#43; taskB * (num - i));
  }

  return &#96;[${[...ans].sort((a, b) &#61;&gt; a - b).join(&#34;, &#34;)}]&#96;;
}
</code></pre> 
<p></p> 
<h4> Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String str &#61; sc.next();
    Integer[] arr &#61; Arrays.stream(str.split(&#34;,&#34;)).map(Integer::parseInt).toArray(Integer[]::new);

    System.out.println(getResult(arr[0], arr[1], arr[2]));
  }

  public static String getResult(int taskA, int taskB, int num) {
    if (num &#61;&#61; 0) return &#34;[]&#34;;

    if (taskA &#61;&#61; taskB) {
      return Arrays.toString(new int[] {taskA * num});
    }

    TreeSet&lt;Integer&gt; ans &#61; new TreeSet&lt;&gt;();
    for (int i &#61; 0; i &lt;&#61; num; i&#43;&#43;) {
      ans.add(taskA * i &#43; taskB * (num - i));
    }

    return ans.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
taskA, taskB, num &#61; map(int, input().split(&#34;,&#34;))


# 算法入口
def getResult(taskA, taskB, num):
    if num &#61;&#61; 0:
        return &#34;[]&#34;

    if taskA &#61;&#61; taskB:
        return [taskA * num]

    ans &#61; set()
    for i in range(num &#43; 1):
        ans.add(taskA * i &#43; taskB * (num - i))

    return sorted(ans)


# 算法调用
print(getResult(taskA, taskB, num))</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>